package com.kreao.korean.repository;

import com.kreao.korean.entity.DatabaseMigration;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

/**
 * 数据库迁移记录Repository
 */
@Repository
public interface DatabaseMigrationRepository extends JpaRepository<DatabaseMigration, Long> {
    
    /**
     * 根据版本号查找迁移记录
     */
    Optional<DatabaseMigration> findByVersion(String version);
    
    /**
     * 查找所有成功执行的迁移记录，按版本号排序
     */
    @Query("SELECT dm FROM DatabaseMigration dm WHERE dm.status = 'SUCCESS' ORDER BY dm.version")
    List<DatabaseMigration> findAllSuccessfulMigrations();
    
    /**
     * 查找最新的迁移版本
     */
    @Query("SELECT dm FROM DatabaseMigration dm WHERE dm.status = 'SUCCESS' ORDER BY dm.version DESC LIMIT 1")
    Optional<DatabaseMigration> findLatestMigration();
    
    /**
     * 检查指定版本是否已执行
     */
    boolean existsByVersionAndStatus(String version, DatabaseMigration.MigrationStatus status);
} 